package vn.appsmobi.activity;

import vn.appsmobi.R;
import vn.appsmobi.model.Account;
import vn.appsmobi.model.Account.AccountType;
import vn.appsmobi.ulti.Log;
import vn.appsmobi.ulti.LoginManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;

import com.easy.facebook.android.apicall.GraphApi;
import com.easy.facebook.android.data.User;
import com.easy.facebook.android.error.EasyFacebookError;
import com.easy.facebook.android.facebook.FBLoginManager;
import com.easy.facebook.android.facebook.Facebook;
import com.easy.facebook.android.facebook.LoginListener;
 
public class LoginActivity extends BaseActivity implements LoginListener  {
	Account mAccount;
	private String FB_APP_ID;
	private FBLoginManager fbLoginManager;
	private Object pDialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setDisplayPopUp(false);
		super.setBarTitle("Tài Khoản");
		setCustomContentView(R.layout.activity_login);
		ImageView btnFacebook = (ImageView)findViewById(R.id.btn_login_fb);
		btnFacebook.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Log.v("connect facebook");
				 connectFacebook();
				
			}
		});
		
		
		/*LoginButton btnFacebookLogin = (LoginButton) findViewById(R.id.btn_login_fb);
		btnFacebookLogin.setReadPermissions(Arrays.asList("basic_info","email"));
		
		if(LoginManager.getManager().hasLogin()){
			LoginManager.getManager().onLoginSuccess(mAccount);
			finish();
		}
		
		
		btnFacebookLogin.setSessionStatusCallback(new Session.StatusCallback() {			  
	    @SuppressWarnings("deprecation")
		@Override
	  
	     public void call(Session session, SessionState state, Exception exception) {
	  
	      if (session.isOpened()) {
	 
	                Log.v("Facebook Access Token"+ session.getAccessToken());
	 
	                Request.executeMeRequestAsync(session,
	 
                        new Request.GraphUserCallback() {
 
                            @Override
 
                            public void onCompleted(GraphUser user,Response response) {
  
                                if (user != null) {
                                Account mAccount = new Account();
                                mAccount.setmAccountType(AccountType.FACEBOOK);
                                mAccount.setmUserEmail(user.asMap().get("email").toString());
                                mAccount.setmUserName(user.getUsername());
                                LoginManager.getManager().setAccount(mAccount);
                                LoginActivity.this.finish();
                                LoginManager.getManager().onLoginSuccess(mAccount);
                                
                                }
  
                            }
 
                        });	  
	         }
	     }
	 
	    });
		*/

	}
	
	private void connectFacebook()
	  {
	    String[] permission = new String[2];
	    permission[0] = "email"; 
	    permission[1] = "publish_stream";	   
	    this.FB_APP_ID = getResources().getString(R.string.app_id);
	    this.fbLoginManager = new FBLoginManager(this, R.layout.activity_login, this.FB_APP_ID, permission);
	    if (this.fbLoginManager.existsSavedFacebook())
	      this.fbLoginManager.loadFacebook();
	    else
	      this.fbLoginManager.login();
	    
	  }

	

	 
	@Override
	 protected void onActivityResult(int requestCode, int resultCode, android.content.Intent data){
	  fbLoginManager.loginSuccess(data);
	 }

	
	/* (non-Javadoc)
	 * @see android.support.v4.app.FragmentActivity#onResume()
	 */
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if(LoginManager.getManager().hasLogin()){	
			if(mAccount!=null){
				//LoginManager.getManager().onLoginSuccess(mAccount);
			}
			finish();
		}
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.login, menu);
		return true;
	}

	@Override
	public void loginSuccess(Facebook facebook) {
	
		new getFacebookInfo().execute(facebook);

	}

	public class getFacebookInfo extends AsyncTask<Facebook,Object,User>{
		Facebook facebook;
		GraphApi graphApi = null;
		 
		
		/* (non-Javadoc)
		 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
		 */
		@Override
		protected void onPostExecute(User user) {
		
		   Account mAccount = new Account();
           mAccount.setmAccountType(AccountType.FACEBOOK);
           mAccount.setmUserEmail(user.getEmail());
           mAccount.setmUserName(user.getName());
           LoginManager.getManager().setAccount(mAccount);
           LoginActivity.this.finish();
           LoginManager.getManager().onLoginSuccess(mAccount);          
		   finish();
		}

		@Override
		protected User doInBackground(Facebook... facebook) {
			// TODO Auto-generated method stub
			graphApi = new GraphApi(facebook[0]);
			User user = new User();
			try {
				user = graphApi.getMyAccountInfo();
			} catch (EasyFacebookError e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return user;
		}
		
	}
	
	@Override
	public void logoutSuccess() {
		 
		
	}

	@Override
	public void loginFail() {
		 
		
	}

	

}
